package cn.whr.server1.service.impl;

import cn.whr.server1.entity.ResponseDTO;
import cn.whr.server1.entity.UserEntity;
import cn.whr.server1.repository.UserRepository;
import cn.whr.server1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserRepository userRepository;

//    @Value("${version}") TODO bootstrap.yml配置连不上springconfig  ,springconfig工程拿得到值
//    private String v;

    @Override
    public ResponseDTO doStep1(UserEntity user) {
        UserEntity existUser = userRepository.findRegisteredByName(user.getName());

        if (existUser== null) {
            // TODO 通过配置取到多个值,随机取出一个
            String code = "6666";

            // TODO 发送短信  phone+code

            user.setCurrentCode(code);
            user.setStatus("registration");
            UserEntity entity = userRepository.save(user);

            return new ResponseDTO("SUCCESS", "短信已发送!pid=" + entity.getPid() + ";code=" + entity.getCurrentCode());
        } else {
            return new ResponseDTO("ERROR", "用户名已存在,请重新输入!");
        }

    }

    @Override
    public ResponseDTO doStep2(UserEntity user) {// 密码可加密
        if (userRepository.updatePwdByPid(user.getPassWord() + "jiami", user.getPid(), "registered") == 1) {
            userRepository.deleteExceptPid(user.getPid(), user.getName());
            return new ResponseDTO("SUCCESS", "注册成功,跳转登录页面!");
        } else {
            return new ResponseDTO("ERROR", "出现未知错误,请稍后联系管理员!");
        }
    }

    @Override
    public ResponseDTO doStep3(UserEntity user) {
        UserEntity existUser = userRepository.findRegisteredByName(user.getName());
        if (existUser == null) {
            return new ResponseDTO("ERROR", "登录失败,帐号不存在!");
        } else if ((user.getPassWord() + "jiami").equals(existUser.getPassWord())) {
            return new ResponseDTO("SUCCESS", "登录成功!");
        } else {
            return new ResponseDTO("ERROR", "登录失败,帐号或密码错误!");
        }
    }

}
